package com.tobeface.wechat.core.timestamp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author loudyn
 */
public abstract class AbstractTimestampService implements WeChatTimestampService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final Random random = new Random();
    private final int[] randomBoundary = new int[]{32, 32 * 32 * 32};

    @Override
    public final String nextTimestamp() {
        return String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    }

    @Override
    public final String nextNonce() {
        int nonce = randomBoundary[0] + random.nextInt(randomBoundary[1] - randomBoundary[0] + 1);
        return String.valueOf(nonce);
    }

    /**
     *
     * @return
     */
    protected Logger getLogger() {
        return logger;
    }
}
